package com.xxx.kjds.controller;

import com.xxx.kjds.entity.*;
import com.xxx.kjds.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private WishlistService wishlistService;
    @Autowired
    private WishGoodsService wishGoodsService;
    @Autowired
    private StoreGoodsService storeGoodsService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ClassificationService classificationService;

    //获取所有上架商品
    @PostMapping(value = "/getAllGroudingGoods")
    @ResponseBody
    public List<Goods> getAllGroudingGoods() {
        return goodsService.selectByState("A");
    }

    //获取所有上架商品
    @RequestMapping("/getAllGroudingGoodsByPage")
    @ResponseBody
    public List<Goods> getAllGroudingGoodsByPage(HttpServletRequest request) {
        Integer pageNo = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        List<Goods> result = new ArrayList();
        List<Goods> list = goodsService.selectByState("A");

        for(int i = 0; i < list.size(); ++i) {
            if (i >= (pageNo - 1) * pageSize && i < pageNo * pageSize) {
                result.add(list.get(i));
            }
        }
        return result;
    }

    //添加商品到心愿单
    @PostMapping(value = "/goodsToWish")
    @ResponseBody
    public String goodsToWish(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        //根据uid遍历wishlist，是否已存在心愿单
        WishList WishList = wishlistService.selectByUid(uid);
        if (WishList == null) {
            WishList WishList1 = new WishList();
            WishList1.setId(uid);
            WishList1.setBvoId(uid);
            wishlistService.insert(WishList1);
        }
        Integer gid = Integer.valueOf(request.getParameter("gid"));
        WishGoodsKey WishGoodsKey = new WishGoodsKey();
        WishGoodsKey.setGoodsId(gid);
        WishGoodsKey.setWishId(uid);
        int result = wishGoodsService.insert(WishGoodsKey);
        if (result > 0) {
            return "添加到心愿单成功";
        }
        return "已存在添加失败";
    }

    //添加商品到店铺
    @PostMapping(value = "/goodsToStore")
    @ResponseBody
    public String goodsToStore(HttpServletRequest request) {
        Integer gid = Integer.valueOf(request.getParameter("gid"));
        Integer sid = Integer.valueOf(request.getParameter("sid"));
        StoreGoods StoreGoods = new StoreGoods();
        StoreGoods.setGoodsid(gid);
        StoreGoods.setStoreid(sid);
        int result = storeGoodsService.insert(StoreGoods);
        if (result <= 0) {
            return "添加到店铺失败";
        }
        return "添加到店铺成功";
    }


    //gid找商品
    @PostMapping("/searchGoodsByID")
    @ResponseBody
    public Goods searchGoodsByID(Integer id) {
        return goodsService.selectByPrimaryKey(id);
    }

    /*public int insertGoodsProfilePhoto(Integer image) {
        return 1;
    }*/

    @PostMapping(value = "/addGoods")
    @ResponseBody
    public String addGoods(String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String brandclassificationname, String brandname) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods goods = new Goods(title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, classification.getId(), brand.getId());
        goods.setState("S");
        int re = goodsService.insert(goods);
        if (re == 1) return "添加商品讯息成功";
        return "添加商品讯息失败";
    }

    @PostMapping(value = "/addGoodsWithPic")
    @ResponseBody
    public String addGoods(String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String brandclassificationname, String brandname, String image) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods goods = new Goods(title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, image, classification.getId(), brand.getId());
        goods.setState("S");

        System.out.println(image);
        System.out.println(goods.getImage());


        int re = goodsService.insert(goods);
        if (re == 1) return "添加商品讯息成功";
        return "添加商品讯息失败";
    }

    @PostMapping("/selectGoodsByTitle")
    @ResponseBody
    public Goods selectGoodsByTitle(String title) {
        return goodsService.selectByTitle(title);
    }

    @PostMapping("/deleteGoods")
    @ResponseBody
    public String deleteGoods(Integer id) {
        int re = goodsService.deleteByPrimaryKey(id);
        if (re == 1) {
            System.out.println("删除商品成功");
            return "删除商品成功";
        }
        System.out.println("删除商品失败");
        return "删除商品失败";
    }

    @PostMapping(value = "/getAllGoodsByBvoId")
    public List<Goods> getAllGoods(Integer brandId) {
        return goodsService.selectAllByBrandId(brandId);
    }

    @PostMapping("/updateGoodsWithPic")
    @ResponseBody
    public String updateGoods(Integer id, String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String image, String state, String brandclassificationname, String brandname) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods record = new Goods(id, title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, image, state, classification.getId(), brand.getId());
        record.setState("S");
        int re = goodsService.updateByPrimaryKey(record);
        if (re == 1) return "更新商品讯息成功";
        return "更新商品讯息失败";
    }

    @PostMapping("/updateGoods")
    @ResponseBody
    public String updateGoods(Integer id, String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String state, String brandclassificationname, String brandname) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods record = new Goods(id, title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, state, classification.getId(), brand.getId());
        record.setState("S");
        int re = goodsService.updateByPrimaryKey(record);
        if (re == 1) return "更新商品讯息成功";
        return "更新商品讯息失败";
    }

    @PostMapping("/uploadToStorehouse")
    @ResponseBody
    public String uploadToStorehouse(Integer id) {
        int re = goodsService.uploadToStorehouse(id);
        if (re == 1) return "商品成功入仓";
        return "商品入仓失败, 此商品并没有录入";
    }

    @PostMapping("/uploadToStore")
    @ResponseBody
    public String uploadToStore(Integer id) {

        int re = goodsService.uploadToStore(id);
        if (re == 1) return "商品成功上架";
        return "商品上架失败, 仓库不存在此商品";
    }

    @PostMapping("/goOffFromStore")
    @ResponseBody
    public String goOffFromStore(Integer id) {
        int re = goodsService.goOffFromStore(id);
        if (re == 1) return "商品成功下架";
        return "商品下架失败，商店不存在此商品";
    }

    @PostMapping("/offstorehouse")
    @ResponseBody
    public String offStorehouse(Integer id) {
        int re = goodsService.offStorehouse(id);
        if (re == 1) return "商品成功下仓";
        return "商品下仓失败，商店不存在此商品";
    }

    @PostMapping("/selectGoodsByCompanyID")
    @ResponseBody
    public List<Goods> selectGoodsByCompanyID(Integer mvo_id) {
        return goodsService.selectGoodsByCompanyID(mvo_id);
    }
}
